﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Utilities.QuickSql;
using MySql.Data;
using System.Threading;

namespace Deviseur_Cs2i_Car
{
    public partial class AddSSOption : Form
    {
        private SSOPTION SSOption;

        public AddSSOption()
        {
            InitializeComponent();

            this.Text = "Ajouter une sous option";
            this.ButtonAddSSOption.Text = "Ajouter";

            DataTable DataOption = new DataTable();

            DataOption.Columns.Add("NumOption");
            DataOption.Columns.Add("LibelleOption");
            DataOption.Rows.Add("0", "- Choisir une categorie -");

            Table optn = Program.Db.QueryTable("SELECT * FROM OPTIONS");
            foreach (Record r in optn)
                DataOption.Rows.Add(r.Values[0], r.Values[1]);

            this.comboBoxSSOption.DataSource = DataOption;
            this.comboBoxSSOption.ValueMember = "NumOption";
            this.comboBoxSSOption.DisplayMember = "LibelleOption";

            this.comboBoxSSOption.SelectedIndex = 0;
        }
        public AddSSOption(int NumSSOption)
        {
            InitializeComponent();

            this.Text = "Ajouter une sous option";
            this.ButtonAddSSOption.Text = "Modifier";

            DataTable DataOption = new DataTable();

            DataOption.Columns.Add("NumOption");
            DataOption.Columns.Add("LibelleOption");
            DataOption.Rows.Add("0", "- Choisir une categorie -");

            Table optn = Program.Db.QueryTable("SELECT * FROM OPTIONS");
            foreach (Record r in optn)
                DataOption.Rows.Add(r.Values[0], r.Values[1]);

            this.comboBoxSSOption.DataSource = DataOption;
            this.comboBoxSSOption.ValueMember = "NumOption";
            this.comboBoxSSOption.DisplayMember = "LibelleOption";

            this.comboBoxSSOption.SelectedIndex = 0;
            /*******/
            Table toto = Program.Db.QueryTable("SELECT * FROM SSOPTION WHERE NumSSOption=" + NumSSOption);
            foreach (Record r in toto)
            {
                this.NomSSOption.Text = r.Values[1].ToString();
                this.textPrixSSOption.Text = r.Values[2].ToString();
                this.comboBoxSSOption.SelectedValue = r.Values[3].ToString();
                SSOption = new SSOPTION(NumSSOption, this.NomSSOption.Text.ToString(), Convert.ToDouble(this.textPrixSSOption.Text), Convert.ToInt32(this.comboBoxSSOption.SelectedValue));
            }

            this.comboBoxSSOption.SelectedIndex = 0;
        }


        private void addSSOption_Thread()
        {
           Invoke(new MethodInvoker(Disabled));
            SSOption.new_SSOption();
           Invoke(new MethodInvoker(CloseFen));
        }

        private void updateSSOption_Thread()
        {
            Invoke(new MethodInvoker(Disabled));
            SSOption.modify_SSOption();
            Invoke(new MethodInvoker(CloseFen));
        }

        private void Disabled()
        {
            this.NomSSOption.Enabled = false;
            this.ButtonAddSSOption.Enabled = false;
        }

        private void CloseFen()
        {
            this.Close();
        }

        private void ButtonAddSSOption_Click_1(object sender, EventArgs e)
        {
            if (NomSSOption.Text.ToString() != "" && textPrixSSOption.Text.ToString() != "" && this.comboBoxSSOption.SelectedValue.ToString() != "")
            {
                if (this.ButtonAddSSOption.Text == "Ajouter")
                {
                    SSOption = new SSOPTION(NomSSOption.Text.ToString(), Convert.ToDouble(textPrixSSOption.Text.ToString()), Convert.ToInt32(this.comboBoxSSOption.SelectedValue.ToString()));

                    Thread addThread = new Thread(new ThreadStart(addSSOption_Thread));
                    addThread.Start();
                }
                else if (this.ButtonAddSSOption.Text == "Modifier")
                {
                    SSOption.set_NomSSOption(this.NomSSOption.Text.ToString());
                    SSOption.set_PrixSSOption(Convert.ToDouble(textPrixSSOption.Text.ToString()));
                    SSOption.set_Option(new OPTION(Convert.ToInt32(this.comboBoxSSOption.SelectedValue.ToString())));
                    Thread updateThread = new Thread(new ThreadStart(updateSSOption_Thread));
                    updateThread.Start();
                }
            }
            else
            {
                MessageBox.Show("Veuillez remplir tous les champs", "Erreur de saisie");
            }
        }
    }

}